package com.bestcem.xm.om.dao.impl;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.om.controller.param.MessageParam;
import com.bestcem.xm.om.controller.param.NoticeInfoParam;
import com.bestcem.xm.om.dao.EmailRecordDao;
import com.bestcem.xm.om.dao.mapper.EmailRecordMapper;
import com.bestcem.xm.om.entity.EmailRecord;
import com.bestcem.xm.om.enums.SendStatusEnum;
import com.bestcem.xm.om.service.dto.common.PageDTO;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author zq.ding <zq.ding@idiaoyan.com>
 * @version v1.0
 * @date 2021/11/11 13:35
 */
@Repository
public class EmailRecordDaoImpl implements EmailRecordDao {
    @Resource
    private EmailRecordMapper emailRecordMapper;

    @Override
    public PageDTO<EmailRecord> listByMessageParam(MessageParam param, List<String> ids) {
        PageDTO<EmailRecord> pageDTO = new PageDTO<>();
        pageDTO.setTotal(emailRecordMapper.countByMessageParam(param, ids));
        if (Objects.nonNull(param.getPage()) && Objects.nonNull(param.getSize())) {
            param.setOffset((param.getPage() - 1) * param.getSize());
        }
        pageDTO.setData(emailRecordMapper.listByMessageParam(param, ids));
        return pageDTO;
    }

    @Override
    public Long countByCondition(NoticeInfoParam noticeInfoParam) {
        return emailRecordMapper.countByCondition(noticeInfoParam);
    }

    @Override
    public List<EmailRecord> listRecordsByCondition(NoticeInfoParam noticeInfoParam) {
        if (Objects.nonNull(noticeInfoParam.getPage())
                && Objects.nonNull(noticeInfoParam.getSize())) {
            noticeInfoParam.setOffset((noticeInfoParam.getPage() - 1) * noticeInfoParam.getSize());
        }
        return emailRecordMapper.listRecordsByCondition(noticeInfoParam);
    }

    @Override
    public Integer batchSave(List<EmailRecord> emailRecordList) {
        emailRecordList.forEach(emailRecord -> emailRecord.setId(ObjectId.next()));
        return emailRecordMapper.batchSave(emailRecordList);
    }

    @Override
    public List<EmailRecord> listByBatchId(String batchId) {
        return emailRecordMapper.listByBatchId(batchId);
    }

    @Override
    public Long removeByBatchId(String batchId) {
        return emailRecordMapper.removeByBatchId(batchId);
    }

    @Override
    public void update(EmailRecord emailRecord) {
        emailRecordMapper.update(emailRecord);
    }

    @Override
    public List<EmailRecord> listByIds(List<String> idList) {
        return emailRecordMapper.listByIds(idList);
    }

    @Override
    public EmailRecord findById(String id) {
        return emailRecordMapper.findById(id);
    }

    @Override
    public void updateSendStatus(String batchId, List<String> idList, SendStatusEnum sendStatusEnum) {
        emailRecordMapper.updateSendStatus(batchId, idList, sendStatusEnum.getIndex(), LocalDateTime.now());
    }

    @Override
    public List<EmailRecord> listByVistedTime(LocalDateTime from) {
        return emailRecordMapper.listByVistedTime(from);
    }

    @Override
    public void insert(EmailRecord emailRecord) {
        emailRecord.setId(ObjectId.next());
        emailRecordMapper.insert(emailRecord);
    }

    @Override
    public Long countLinkVisited(String batchId) {
        return emailRecordMapper.countLinkVisited(batchId);
    }

    @Override
    public Long countSuccess(String batchId) {
        return emailRecordMapper.countSuccess(batchId);
    }

    @Override
    public Long countByStatus(String batchId, SendStatusEnum sendStatusEnum) {
        return emailRecordMapper.countByStatus(batchId, sendStatusEnum.getIndex());

    }
}
